package com.example.goandpaytthebill.Activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.goandpaytthebill.Adapter.FragmentChangeAdapter;
import com.example.goandpaytthebill.Fragment.GrabbingOrdersFragment;
import com.example.goandpaytthebill.Fragment.MyFragment;
import com.example.goandpaytthebill.Fragment.NewHomePageFragment;
import com.example.goandpaytthebill.Fragment.OrderFragment;
import com.example.goandpaytthebill.R;
import com.example.goandpaytthebill.Tool.XViewPager;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private LinearLayout ll_main_wd;
    private XViewPager main_viewpager;
    private RelativeLayout ll_main_sy;
    private LinearLayout ll_main_ss;

    private LinearLayout ll_main_video;
    private LinearLayout ll_main_gwc;

    private ImageView main_sy;
    private ImageView main_ss;

    private ImageView main_gw;
    private ImageView main_wd;
    private TextView main_tv_sy;
    private TextView main_tv_ss;

    private TextView main_tv_gwc;
    private TextView main_tv_wd;
    private ArrayList<Fragment> flist;
    private FragmentChangeAdapter fadapter;
    private LinearLayout ll_sy_wxz;
    private ImageView main_sy_xz;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            //修改为深色，因为我们把状态栏的背景色修改为主题色白色，默认的文字及图标颜色为白色，导致看不到了。
            MainActivity.this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
        setContentView(R.layout.activity_main);
        setView();
        initView();


    }

    private void initView() {
        ll_main_wd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this,LoginActivity.class);
                startActivity(intent);
            }
        });
        flist = new ArrayList<>();//这里如果不写的话,就没有存放Fragment的list了

        //是会报错的
        flist.add(new NewHomePageFragment());
        flist.add(new GrabbingOrdersFragment());
        flist.add(new OrderFragment());
        flist.add(new MyFragment());

        ll_main_sy.setOnClickListener(this);
        ll_main_ss.setOnClickListener(this);
        ll_main_gwc.setOnClickListener(this);
        ll_main_wd.setOnClickListener(this);
        ll_main_video.setOnClickListener(this);
        fadapter=new FragmentChangeAdapter(getSupportFragmentManager(),flist);
        main_viewpager.setAdapter(fadapter);
        main_viewpager.setOffscreenPageLimit(5);

        }


    private void setView() {
        main_viewpager=findViewById(R.id.main_viewpager);
        ll_main_sy=findViewById(R.id.ll_main_sy);
        ll_main_ss=findViewById(R.id.ll_main_ss);
        ll_main_video=findViewById(R.id.ll_main_video);
        ll_main_gwc=findViewById(R.id.ll_main_gwc);
        ll_main_wd=findViewById(R.id.ll_main_wd);
        main_sy=findViewById(R.id.main_sy);
        main_ss=findViewById(R.id.main_ss);

        main_gw=findViewById(R.id.main_gw);
        main_wd=findViewById(R.id.main_wd);
        main_tv_sy=findViewById(R.id.main_tv_sy);
        main_tv_ss=findViewById(R.id.main_tv_ss);

        main_tv_gwc=findViewById(R.id.main_tv_gwc);
        main_tv_wd=findViewById(R.id.main_tv_wd);
        ll_sy_wxz=findViewById(R.id.ll_sy_wxz);
        main_sy_xz=findViewById(R.id.main_sy_xz);
        ll_sy_wxz.setVisibility(View.GONE);
        main_sy_xz.setVisibility(View.VISIBLE);
        main_viewpager.setCurrentItem(0);// 跳到第一个页面

        main_ss.setImageResource(R.mipmap.qiangdan_logo);
        main_wd.setImageResource(R.mipmap.wode_logo);
        main_gw.setImageResource(R.mipmap.order_logo);

        main_tv_ss.setTextColor(Color.parseColor("#808080"));
        main_tv_gwc.setTextColor(Color.parseColor("#808080"));
        main_tv_wd.setTextColor(Color.parseColor("#808080"));

        setStatusBarFullTransparent();
        setFitSystemWindow(false);
    }

    //退出时的时间
    private long mExitTime;
    //对返回键进行监听
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {

            exit();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void exit() {
        if ((System.currentTimeMillis() - mExitTime) > 2000) {
            Toast.makeText(MainActivity.this, "再按一次退出", Toast.LENGTH_SHORT).show();
            mExitTime = System.currentTimeMillis();
        } else {
//            MyConfig.clearSharePre(this, "users");
            finish();
            System.exit(0);
        }
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_main_sy:
                ll_sy_wxz.setVisibility(View.GONE);
                main_sy_xz.setVisibility(View.VISIBLE);
                main_viewpager.setCurrentItem(0);// 跳到第一个页面

                main_ss.setImageResource(R.mipmap.qiangdan_logo);
                main_wd.setImageResource(R.mipmap.wode_logo);
                main_gw.setImageResource(R.mipmap.order_logo);

                main_tv_ss.setTextColor(Color.parseColor("#808080"));
                main_tv_gwc.setTextColor(Color.parseColor("#808080"));
                main_tv_wd.setTextColor(Color.parseColor("#808080"));

                setStatusBarFullTransparent();
                setFitSystemWindow(false);
                break;
            case R.id.ll_main_ss:
                ll_sy_wxz.setVisibility(View.VISIBLE);
                main_sy_xz.setVisibility(View.GONE);

                main_viewpager.setCurrentItem(1);// 跳到第一个页面


                main_ss.setImageResource(R.mipmap.qiangdan_xz);
                main_wd.setImageResource(R.mipmap.wode_logo);
                main_gw.setImageResource(R.mipmap.order_logo);

                main_tv_ss.setTextColor(Color.parseColor("#FF8D30"));
                main_tv_gwc.setTextColor(Color.parseColor("#808080"));
                main_tv_wd.setTextColor(Color.parseColor("#808080"));

                //状态栏中的文字颜色和图标颜色，需要android系统6.0以上，而且目前只有一种可以修改（一种是深色，一种是浅色即白色）
                if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    //修改为深色，因为我们把状态栏的背景色修改为主题色白色，默认的文字及图标颜色为白色，导致看不到了。
                    MainActivity.this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                }
                break;

            case R.id.ll_main_gwc:
                ll_sy_wxz.setVisibility(View.VISIBLE);
                main_sy_xz.setVisibility(View.GONE);
                main_viewpager.setCurrentItem(3);// 跳到第一个页面

                main_ss.setImageResource(R.mipmap.qiangdan_logo);
                main_wd.setImageResource(R.mipmap.wode_logo);
                main_gw.setImageResource(R.mipmap.order_xz);

                main_tv_ss.setTextColor(Color.parseColor("#808080"));
                main_tv_gwc.setTextColor(Color.parseColor("#FF8D30"));
                main_tv_wd.setTextColor(Color.parseColor("#808080"));

                //状态栏中的文字颜色和图标颜色，需要android系统6.0以上，而且目前只有一种可以修改（一种是深色，一种是浅色即白色）
                if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    //修改为深色，因为我们把状态栏的背景色修改为主题色白色，默认的文字及图标颜色为白色，导致看不到了。
                    MainActivity.this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                }
                break;
            case R.id.ll_main_wd:
                ll_sy_wxz.setVisibility(View.VISIBLE);
                main_sy_xz.setVisibility(View.GONE);
                setStatusBarFullTransparent();
                setFitSystemWindow(false);
                main_viewpager.setCurrentItem(4);// 跳到第一个页面

                main_ss.setImageResource(R.mipmap.qiangdan_logo);
                main_wd.setImageResource(R.mipmap.geren_xz);
                main_gw.setImageResource(R.mipmap.order_logo);

                main_tv_ss.setTextColor(Color.parseColor("#808080"));
                main_tv_gwc.setTextColor(Color.parseColor("#808080"));
                main_tv_wd.setTextColor(Color.parseColor("#FF8D30"));

                break;
            default:
                break;
        }
    }
    private void setStatusBarFullTransparent() {
        if (Build.VERSION.SDK_INT >= 21) {//21表示5.0
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= 19) {//19表示4.4
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //虚拟键盘也透明
            //getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }

    }
    private View contentViewGroup;
    protected void setFitSystemWindow(boolean fitSystemWindow) {
        if (contentViewGroup == null) {
            contentViewGroup = ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
        }
        contentViewGroup.setFitsSystemWindows(fitSystemWindow);
    }
}